package com.zxpan.restaurant.persist.dao.impl;

import java.lang.reflect.Array;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.defaults.DefaultSqlSessionFactory;

import com.zxpan.restaurant.persist.dao.Dao;


public class DaoBase<T> implements Dao<T> {

	 protected DefaultSqlSessionFactory sqlSessionFactory;
     
     private String prefix;
     private Class<T> clazz;
     
     @Override
     public T[] select(Map<String, String> map) {
             SqlSession session = sqlSessionFactory.openSession(true);
             @SuppressWarnings("unchecked")
             List<T> resultList = (List<T>)session.selectList(new StringBuilder(prefix).append(".select").toString(), map);
             if(resultList == null || resultList.size() == 0) {
                     return null;
             } 
             @SuppressWarnings("unchecked")
             T[] resultArray = (T[]) Array.newInstance(clazz, resultList.size());
             for(int i = 0, length = resultList.size(); i < length; i++) {
                     resultArray[i] = resultList.get(i);
             }
             session.close();
             session = null;
             return resultArray;
     }

     @Override
     public int update(T t) {
             SqlSession session = sqlSessionFactory.openSession(true);
             int result = session.update((new StringBuilder(prefix).append(".update").toString()), t);
             session.close();
             session = null;
             return result;
     }

     @Override
     public int add(T t) {
             SqlSession session = sqlSessionFactory.openSession(true);
             int result = session.insert((new StringBuilder(prefix).append(".insert").toString()), t);
             session.close();
             session = null;
             return result;
     }

     @Override
     public int deleteById(int id) {
             SqlSession session = sqlSessionFactory.openSession(true);
             int result = session.delete((new StringBuilder(prefix).append(".deleteById").toString()), id);
             session.close();
             session = null;
             return result;
     }

     public String getPrefix() {
             return prefix;
     }

     public void setPrefix(String prefix) {
             this.prefix = prefix;
     }

     public Class<T> getClazz() {
             return clazz;
     }

     public void setClazz(Class<T> clazz) {
             this.clazz = clazz;
     }

     @SuppressWarnings("unchecked")
     @Override
     public T selectObject(T t) {
             SqlSession session = sqlSessionFactory.openSession(true);
             T result  = (T) session.selectOne((new StringBuilder(prefix).append(".selectObject").toString()), t);
             session.close();
             session = null;
             return result;
     }

     
     
     public DefaultSqlSessionFactory getSqlSessionFactory() {
             return sqlSessionFactory;
     }

     public void setSqlSessionFactory(DefaultSqlSessionFactory sqlSessionFactory) {
             this.sqlSessionFactory = sqlSessionFactory;
     }

     @Override
     public T[] selectObjectList(T t) {
             SqlSession session = sqlSessionFactory.openSession(true);
             @SuppressWarnings("unchecked")
             List<T> resultList = (List<T>)session.selectList(new StringBuilder(prefix).append(".selectObjectList").toString(), t);
             session.close();
             session = null;
             if(resultList == null || resultList.size() == 0) {
                     return null;
             } 
             @SuppressWarnings("unchecked")
             T[] resultArray = (T[]) Array.newInstance(clazz, resultList.size());
             for(int i = 0, length = resultList.size(); i < length; i++) {
                     resultArray[i] = resultList.get(i);
             }
             return resultArray;
     }

     @Override
     public T[] selectById(Integer id) {
             SqlSession session = sqlSessionFactory.openSession(true);
             @SuppressWarnings("unchecked")
             List<T> resultList = (List<T>)session.selectList(new StringBuilder(prefix).append(".selectById").toString(), id);
             session.close();
             session = null;
             if(resultList == null || resultList.size() == 0) {
                     return null;
             } 
             @SuppressWarnings("unchecked")
             T[] resultArray = (T[]) Array.newInstance(clazz, resultList.size());
             for(int i = 0, length = resultList.size(); i < length; i++) {
                     resultArray[i] = resultList.get(i);
             }
             return resultArray;
     }

}
